package com.bsol.hss.service;

import java.util.List;

import com.bsol.hss.bean.Book;
import com.bsol.hss.bean.Order;
import com.bsol.hss.bean.Page;
import com.bsol.hss.bean.TrueOrder;
import com.bsol.hss.bean.User;
import com.bsol.hss.dao.BookDao;
import com.bsol.hss.dao.OrderDao;
import com.bsol.hss.dao.UserDao;
import com.bsol.hss.dao.impl.BookDaoImpl;
import com.bsol.hss.dao.impl.OrderDaoImpl;
import com.bsol.hss.dao.impl.UserDaoImpl;

public class BusinessService {
	
	BookDao bookDao=new BookDaoImpl();
	OrderDao orderDao=new OrderDaoImpl();
	UserDao userDao=new UserDaoImpl();
	
	public boolean userLogin(String username,String password) {
		boolean is=false;
		User user=userDao.findUserByName(username);
		if(user!=null) {
			if(user.getPassword().equals(password)) {
				is=true;
			}
		}
		return is;
	}
	
	public User findUserByName(String username) {
		return userDao.findUserByName(username);
	}
	
    public boolean isManager(String username) {
    	boolean is=false;
    	User user=userDao.findUserByName(username);
    		if(user!=null) {
    			if(user.getManagement()==1) {
    				is=true;
    			}
    		}
    	return is;
    }
    
    public int userRegister(User user) {
    	int result=0;
    	User u=userDao.findUserByName(user.getUsername());
    	if(u!=null) {
    		result=-1;
    	}
    	else {
    		if(userDao.addUser(user)) {
    			result=1;
    		}
    	}
    	return result;
    }
    
    public boolean addBook(Book book) {
    	return bookDao.addBook(book);
    }
    
    public Book findBookByName(String bookname) {
    	return bookDao.findBookByName(bookname);
    }
    
    public List<Book> findAllBooks(){
    	return bookDao.findAllBooks();
    }
    
    public boolean updateStockById(Integer stock,Integer bookid) {
    	return bookDao.updateStockById(stock, bookid);
    }
    
    public boolean addOrder(Order order) {
    	return orderDao.addOrder(order);
    }
    
    public List<TrueOrder> findAllOrders(){
    	return orderDao.findAllOrders();
    }
    
    public List<TrueOrder> findOrdersByName(String username){
    	return orderDao.findOrdersByName(username);
    }
    
    public boolean rollBack() {
    	return orderDao.rollBack();
    }
    
    public Page getPageDates(int startindex,int endindex) {
    	Page page=new Page();
    	page.setList(bookDao.getPageDates(startindex, endindex));
    	return page;
    }
    
    public int getBookNum() {   	
    	return bookDao.getBookNum();
    }
    
    public int getOrderNum() {
    	return orderDao.getOrderNum();
    }
    
    public int getUserOrderNum(String username) {
    	return orderDao.getUserOrderNum(username);
    }

    public Page getOrderPageDates(int startindex,int endindex) {
    	Page page=new Page();
    	page.setList(orderDao.getPageDates(startindex, endindex));
    	return page;
    }
    
    public Page getUserOrerPageDates(int startindex,int endindex,String username) {
    	Page page=new Page();
    	page.setList(orderDao.getUserOrdersDates(startindex, endindex, username));
    	return page;
    }
    
    public boolean updateNameByName(String new_name,String username) {
    	return  userDao.updateNameByName(new_name, username);
    }
    
    public boolean updatePasswordByName(String password,String username) {
    	return  userDao.updatePasswordByName(password, username);
    }

}
